package com.hnit.applet.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hnit.applet.common.annotation.AccessLimit;
import com.hnit.applet.common.constant.CommonConstant;
import com.hnit.applet.common.controller.BaseController;
import com.hnit.applet.common.enums.LimitType;
import com.hnit.applet.common.model.CPage;
import com.hnit.applet.common.model.Response;
import com.hnit.applet.common.model.ResponsePage;
import com.hnit.applet.entity.BaseDim;
import com.hnit.applet.service.BaseDimService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zwj
 * @date 2022/7/7
 * @description TODO
 */

@Api(tags = "维度基础信息表对象功能接口")
@RestController
@RequestMapping("/baseDim")
public class BaseDimController extends BaseController {
    @Resource
    private BaseDimService baseDimService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     *
     * @return 实例对象
     */
    @ApiOperation("通过ID查询单条数据")
    @GetMapping("{id}")
    @AccessLimit(seconds = 60, maxCount = 1, type = LimitType.IP)
    public Response<BaseDim> queryById(@PathVariable String id) {
        return getResponse(baseDimService.queryById(id), CommonConstant.Message.COMMON_VALID_L_QUERY, new Object[]{"1111"});
    }

    /**
     * 分页查询
     *
     * @param baseDim 筛选条件
     *
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping
    public ResponsePage<?> query(BaseDim baseDim) {
        CPage<BaseDim> page = new CPage<>(1, 10);
        IPage<BaseDim> retPage = baseDimService.paginQuery(baseDim, page);
        return getResponsePage(page.toPage(), retPage.getRecords(), new Object[]{"合同-字段"});
    }

    /**
     * 新增数据
     *
     * @param baseDim 实例对象
     *
     * @return 实例对象
     */
    @ApiOperation("新增数据")
    @PostMapping
    public ResponseEntity<BaseDim> add(BaseDim baseDim) {
        return ResponseEntity.ok(baseDimService.insert(baseDim));
    }

    /**
     * 更新数据
     *
     * @param baseDim 实例对象
     *
     * @return 实例对象
     */
    @ApiOperation("更新数据")
    @PutMapping
    public ResponseEntity<BaseDim> edit(BaseDim baseDim) {
        return ResponseEntity.ok(baseDimService.update(baseDim));
    }

    /**
     * 通过主键删除数据
     *
     * @param dimId 主键
     *
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String dimId) {
        return ResponseEntity.ok(baseDimService.deleteById(dimId));
    }
}
